package com.hzcx.app.simplechat.util.glide;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.chat.util.BitmapUtil;
import com.hzcx.app.simplechat.chat.util.ImageSize;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

public class GlideUtils {

    public static void loadImgSimple(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.img_placeholder)
                .error(R.drawable.img_placeholder)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .into(imageView);
    }

    /**
     * 简单加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImg(Context context, String url, ImageView imageView) {
        if (context==null){
            return;
        }
        if (context  instanceof Activity && ((Activity) context).isDestroyed()){
            return;
        }
        Glide.with(context)
                .load(url)
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(imageView);
    }


    /**
     * 图片中心显示
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgAsCenter(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.img_placeholder)
                .error(R.drawable.img_placeholder)
                .apply(new RequestOptions().centerCrop())
                .into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgAsCircle(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .apply(new RequestOptions().circleCrop())
                .into(imageView);
    }

    /**
     * 加载带边框的圆形图片
     *
     * @param context
     * @param url
     * @param view
     * @param width
     * @param color
     */
    public static void loadImgAsCircle(Context context, String url, ImageView view, int width, int color) {
        if (EmptyUtils.isNotEmpty(view)) {
            RequestOptions requestOption2;
            requestOption2 = RequestOptions.bitmapTransform(new GlideCircleWithBorder(width, color));
            Glide.with(context)
                    .load(url)
                    .apply(requestOption2)
                    .into(view);
        }
    }

    /**
     * 聊天页面，加载 图片
     *
     * @param context
     * @param url
     * @param view
     */
    public static void loadChatImage(Context context, String url, ImageView view) {
        if (EmptyUtils.isNotEmpty(view)) {
            final RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.img_placeholder)// 正在加载中的图片
                    .error(R.drawable.img_placeholder); // 加载失败的图片
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            ImageSize imageSize = BitmapUtil.getImageSize(((BitmapDrawable) resource).getBitmap());
                            RelativeLayout.LayoutParams imageLP = (RelativeLayout.LayoutParams) (view.getLayoutParams());
                            imageLP.width = imageSize.getWidth();
                            imageLP.height = imageSize.getHeight();
                            view.setLayoutParams(imageLP);

                            view.setImageBitmap(((BitmapDrawable) resource).getBitmap());

//                            Glide.with(context)
//                                    .load(resource)
//                                    .apply(options) // 参数
//                                    .into(view);
                        }
                    });
        }
    }
}
